$(function(){
    // 需求1 渲染头像
    getUserInfo ()

    // 注册事件退出首页
    $('#logout').click(function(){
        layui.layer.confirm('确定退出登录?', {icon: 3, title:'提示'}, function(index){
            //do something
            location.href = '/login.html'
            localStorage.removeItem('token')
            layui.layer.close(index)
          })
    })
})

// 封装全局函数 渲染头像 后续回使用 
function getUserInfo () {
axios({
    url:'/my/userinfo'
}).then(res=>{
    //成功回调
    let {data:{status,message,data}} = res
    if (status != 0) {
        return layui.layer.msg(message)
    }
    console.log(data.user_pic)
    let name = data.nickname || data.username
    $('.username').html(name)
    if (data.user_pic) {
        $('.user-avatar').hide()
        $('.layui-nav-img').show().attr('src',data.user_pic)
    }else {
        $('.user-avatar').show().html(name[0].toUpperCase())
        $('.layui-nav-img').hide()
    }
})
}